package ast;

import java.util.ArrayList;

import compiler.OutputFormatter;

public class Program {
	ArrayList<Function> functions;

    public Program(ArrayList<Function> functions) {
        this.functions = functions;
    }

	/**
	 * Generates the C source code of the current knot
	 * @param of The OutputFormatter where the code will be generated
	 */    
    public void genC(OutputFormatter of) {
        of.outputln("#include<stdio.h>");
        of.outputln("#include<stdlib.h>");
        of.outputln("#include<string.h>");
        of.outputBlankLine();
        of.outputln("#define STR_SIZE 1024");
        of.outputBlankLine();
        of.outputln("char *string_new() { return (char*) calloc(STR_SIZE, sizeof(char)); }");
        of.outputln("void string_attr(char *from, char *to) { strcpy(to, from); }");
        of.outputln("void string_read(char *to) { fgets(to, STR_SIZE, stdin); to[strlen(to)-1] = 0; }");
        of.outputBlankLine();
        for(Function f : functions)
        	f.genC(of);
    }
}
